<template>
  <view>
<!--    <nui-step :step="stepType" :class="{ pt40: isSuccess }"></nui-step>-->
    <u-top-tips ref="uTips" navbar-height="0"></u-top-tips>
    <text class="loading-box" v-if="isShowLoading">
      <u-loading mode="circle" size="150" color="#2b85e4"></u-loading>
      <view class="text">协议生成中，请稍后···</view>
    </text>

    <view class="pdf-box" :class="{ h80: !isSuccess }">
      <show-pdf class="iframe" ref="pdf" v-for="i in numPages" :key="i" :src="url" :page="i"></show-pdf>
    </view>

    <text v-if="!isSuccess" class="fixed">
      <text class="tips">提示：手指上下滑动查看更多内容。</text>
      <u-button type="primary" shape="square" :ripple="true" size="medium" class="custom-btn" @click="goSign">前往签名
      </u-button>
    </text>
    <text v-else class="fixed">
      <u-button type="primary" shape="square" :ripple="true" size="medium" class="custom-btn" @click="goSign('success')">完成
      </u-button>
    </text>
    <u-modal v-model="show" confirm-text="退出"
             title="提示"  @confirm="confirm"
    >
      <view class="u-update-content">
        <rich-text style="text-align: center;padding: 30rpx" :nodes="content"></rich-text>
      </view>
    </u-modal>
  </view>
</template>

<script>
import ShowPdf from "vue-pdf";
import { mapGetters } from "vuex"
import AuthInfo from "@/utils/authInfo.js";
import { getQueryString } from "@/utils/index.js";
import {getUrlParam} from "@/utils";
import * as consts from "@/utils/constant.js";

export default {
  data() {
    return {
      isShowLoading: true,
      url: "", //https://pdf-lib.js.org/assets/with_update_sections.pdf http://xsd.jluomi.com/xsdcrm-wechat-ui/1.pdf
      numPages: null, // pdf 总页数,
      deptId: null,
      isSuccess:false,
      pdfId:'',
      show:false,
      content:'床位已入住或当前角色无法办理入住',
    };
  },
  components: {
    ShowPdf,
  },
  async onLoad(query) {
    let {data} = await this.$api.dormitory.getBedInfoById(uni.getStorageSync('bedId'))
    if (data.bedStatus != 1) {
      this.show = true
      return
    }

    this.isSuccess = query.isSuccess?JSON.parse(query.isSuccess):''
    if (this.isSuccess == true || this.isSuccess == 'true') {
      this.checkIn()
      return
    }
    await this.getPdf();
  },
  methods: {
    confirm() {
      uni.reLaunch({
        url: `/pages/HomePage/webIndex`
      })
    },
    async getPdf() {
        let pdfData = uni.getStorageSync("checkInForm")
        let { code, msg } = await this.$api.dormitory.regenerate({name:pdfData.name,idCardNo:pdfData.idCardNo,phoneNumber:pdfData.phoneNumber,paymentMethod:pdfData.paymentMethod});
        if (code != 200) {
          uni.showToast({
            title: msg,
            icon: "error",
          });
          return false;
        }
        // this.url = '/profilePdf/'+ msg;
        this.url = (window.location.href.includes('/xsdcrm-wechat-ui/')?consts.API_BASE_URL+'/xsdcrm-static/download/': '/profilePdf/') + msg;
        uni.setStorageSync("agreementPath",msg);
        this.getNumPages(this.url);
        // this.showTopTips();
    },
    async checkIn() {
      if (uni.getStorageSync("loadSignPDF")) {
        let DATA = uni.getStorageSync("loadSignPDF")
        let { data } = await this.$api.dormitory.loadSignPDF(DATA);
        if (data.pdfPath == "") {

        } else if (data.isSuccess == "Y") {
          this.isSuccess = false
        }
        // this.url = '/profilePdf/'+ data.pdfPath;
        this.url = (window.location.href.includes('/xsdcrm-wechat-ui/')?consts.API_BASE_URL+'/xsdcrm-static/download/': '/profilePdf/') +data.pdfPath;
        uni.setStorageSync("agreementPath",data.pdfPath);
        this.getNumPages(this.url);
        this.showTopTips();
      }
      let form = uni.getStorageSync("checkInForm")
      console.log("form===",form);
      form.signPath = uni.getStorageSync("signPath")
      form.agreementPath = uni.getStorageSync("agreementPath")
      form.isSign = 'Y'
      form.isCrm = form.isCrm == 1? 1 :0
      console.log("form.sex===",form.sex);
      // form.sex = form.sex == '男'? 0 : 1
      const {code,msg,data} = await this.$api.dormitory.checkIn(form).catch((e)=>{
        uni.hideLoading();
      })
      if (code == 200) {
        let DATA = data
        uni.setStorageSync("loadSignPDF",DATA)
        let res = await this.$api.dormitory.loadSignPDF(DATA);
        let data2 = res.data
        if (data2.pdfPath == "") {

        } else if (data2.isSuccess == "Y") {
          this.isSuccess = false
        }
        // this.url = '/profilePdf/'+ data2.pdfPath;
        this.url = (window.location.href.includes('/xsdcrm-wechat-ui/')?consts.API_BASE_URL+'/xsdcrm-static/download/': '/profilePdf/') +data2.pdfPath;
        uni.setStorageSync("agreementPath",data2.pdfPath);
        this.getNumPages(this.url);
        this.showTopTips();
      }
    },
    showTopTips() {
      if (this.isSuccess) {
        this.$refs.uTips.show({
          title: "签约成功",
          type: "success",
          duration: "230000",
        });
      }
    },
    goSign(type) {
      if (type == 'success') {
        uni.reLaunch({
          url: `/pages/HomePage/webIndex`
        })
        return
      }
      uni.redirectTo({
        url: `/pages/HomePage/sign/webIndex`
      })
    },
    //计算pdf页码总数
    getNumPages(url) {
      let loadingTask = ShowPdf.createLoadingTask(url);
      loadingTask.promise
        .then((pdf) => {
          this.numPages = pdf.numPages;
          setTimeout(() => {
            this.isShowLoading = false;
          }, 1500);
        })
        .catch((err) => {
          uni.showToast({
            title: "协议加载失败",
            icon: "error",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
uni-page-body {
  height: 100%;
}

.u-tips {
  height: auto;
}

.pdf-box {
  position: absolute !important;
  top: 44rpx;
  height: 68vh;
  width:100%;
  overflow-y: scroll;
}

.tips {
  position: absolute;
  bottom: 50px;
  margin: 15px auto;
  left: 50%;
  transform: translateX(-50%);
  width: 80%;
  font-size: 12px;
  color: #666;
  width: 100%;
  text-align: center;
}

.custom-btn {
  position: absolute;
  bottom: 10px;
  margin: 15px auto;
  left: 50%;
  transform: translateX(-50%);
}

.loading-box {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  text-align: center;

  .text {
    margin-top: 10px;
  }
}

.fixed {
  position: fixed;
  width: 100%;
  height: 90px;
  bottom: 0;
  background: #fff;
  border-top: 1px solid #e1e1e1;
}

.pt40 {
  padding-top: 50px;
}

.h80 {
  height: 80vh !important;
}
</style>
